package com.rock.securityresearch.equity

import cats.effect.IO
import com.rock.securityresearch.equity.HistoricalStockQuote
import com.rock.securityresearch.equity.StockCode
import com.rock.securityresearch.equity.StockCode.StockCode
import scalikejdbc.*

import java.time.LocalDate

case class HistoricalStockQuote(code: String, // 股票代码
                                name: String, // 名称
                                date: String, // 日期
                                topen: Option[Double], // 开盘价
                                tclose: Option[Double], // 收盘价
                                pchg: Option[Double], // 涨跌幅
                                chg: Option[Double], // 涨跌额
                                tcap: Option[Double], // 总市值
                                mcap: Option[Double], // 流通市值
                                voturnover: Option[Double], // 成交量
                                vaturnover: Option[Double], // 成交金额
                                turnover: Option[Double] // 换手率
                               )

object HistoricalStockQuote extends SQLSyntaxSupport[HistoricalStockQuote] {

    import com.rock.securityresearch.db.Db.given

    override val tableName = "HistoricalStockQuote"

    def apply(rs: WrappedResultSet): HistoricalStockQuote = {
        HistoricalStockQuote(
            code = rs.string("id"),
            name = rs.string("name"),
            date = rs.string("date"),
            topen = Option(rs.double("topen")),

            tclose = Option(rs.double("tclose")),
            pchg = Option(rs.double("pchg")),
            chg = Option(rs.double("chg")),
            tcap = Option(rs.double("tcap")),

            mcap = Option(rs.double("mcap")),
            voturnover = Option(rs.double("voturnover")),
            vaturnover = Option(rs.double("vaturnover")),
            turnover = Option(rs.double("turnover"))
        )
    }

    def findStocks(code: StockCode, startDate: LocalDate, endDate: LocalDate = LocalDate.now()): IO[List[HistoricalStockQuote]] = {
        IO.blocking(sql"select * from HistoricalStockQuote".map(rs => HistoricalStockQuote(rs)).list.apply())
    }
}



